<template>
  <div>
    <el-form ref="empForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" style="width:80%" placeholder="手机" />
      </el-form-item>

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="请选择入职时间" style="width: 80%;" />
      </el-form-item>

      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option v-for="item in hireTypeList" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          style="width:80%"
          placeholder="请选择部门"
          @focus="showTree"
        />

        <div v-show="isShowTree">
          <el-tree
            :data="list"
            :props="{label:'name'}"
            @node-click="handleNodeClick"
          />
        </div>
        <!-- <el-select v-model="form.departmentName" style="width:80%" placeholder="请选择部门">
          <el-option v-for="(item,index) in departmentList" :key="index" :value="item" :label="item" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" type="date" placeholder="请选择转正时间" style="width: 80%;" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmit">确 定</el-button>
      <el-button @click="doCancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index.js'
import { getDepartments } from '@/api/departments.js'
import employeesENUM from '@/constant/employees.js'
import { addEmployee } from '@/api/employees'
console.log('employeesENUM', employeesENUM.hireType)

export default {
  data() {
    return {
      isShowTree: false, // 部门树的显示与隐藏
      hireTypeList: employeesENUM.hireType,
      departmentList: [],
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      list: [], // 部门列表
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  // async created() {
  //   this.getDepartmentList()
  // },
  methods: {
    // 获取部门列表
    async getDepartmentList() {
      const res = await getDepartments()
      console.log(res)
      this.list = res.data.depts
      this.list.shift()
      this.list = tranListToTreeData(this.list)
    },
    // 点击提交按钮新增/编辑
    async doSubmit() {
      this.$refs.empForm.validate(valid => {
        if (valid) {
          this.doAdd()
        }
      })
    },
    async doAdd() {
      try {
        await addEmployee(this.form)
        this.$message.success('添加成功')
        this.$emit('closeWindow')
      } catch (error) {
        console.log(error)
        this.$message.error('添加失败')
      }
    },
    doCancel() {
      this.$emit('closeWindow')
    },
    // 重置表单
    resetForm() {
      this.$refs.empForm.resetFields()
    },
    // 部门树的显示与隐藏
    showTree() {
      this.isShowTree = true
      this.getDepartmentList()
    },
    handleNodeClick(data) {
      console.log('data', data)

      if (!data.children.length) {
        this.form.departmentName = data.name
        this.isShowTree = false
      }
    }
  }
}
</script>

<style>

</style>
